﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Infrastructures;
using Models;
using System.IO;

namespace SponsorshipWeb
{
    public partial class Book : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //var user = AuthUtil.GetAuthentication();
                //if (user == null)
                //    Response.Redirect("../login.html");

                //if (user.CompanyID == 0)
                //    Response.Redirect("guide.html");

                //hfCompanyID.Value = user.CompanyID.ToString();
                //hfUserID.Value = user.UserID.ToString();

                //string mode = Request.QueryString["mode"];
                //if (mode == "edit")
                //{
                //    hfMode.Value = mode;
                //    hfProjectID.Value = Request.QueryString["id"];
                //    BindUI(hfProjectID.Value);
                //}
            }
        }

        private void BindUI(string id)
        {
            //IProjectService service = ServiceFactory.CreateInstance<IProjectService>();

            //var project = service.Get<Project>(id);

            //hfCompanyID.Value = project.BelongNO.ToString();

            //txtName.Text = project.ProjectName;
            //txtMoney.Text = project.Money.ToString();
            //txtStartTime.Text = project.StartTime.ToString(DataFormatter.ShortDate);
            //txtEndTime.Text = project.EndTime.ToString(DataFormatter.ShortDate);
            //ddlCount.SelectedValue = project.Entrants.ToString();
            //txtRemark.Text = project.ProjectRequire;
            //txtUserName.Text = project.UrgentRemark;

            //var lstExtend = service.ListProjectExtend(project.ProjectNO, ProjectExtendFun.All);
            //SetValue(lstExtend, ProjectExtendFun.Material);
            //SetValue(lstExtend, ProjectExtendFun.Type);
            //SetValue(lstExtend, ProjectExtendFun.City);
            //SetValue(lstExtend, ProjectExtendFun.AgentPassword);

            //IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //var lst = utilService.ListFileSystem(project.ProjectNO);
            //FileSystem file = lst.FirstOrDefault(x => x.Fun == FileType.SponsorBook);
            //if (file == null)
            //    return;
            //hlBook.NavigateUrl = file.Path;
            //hlBook.Text = file.CtreateTime.ToString(DataFormatter.ShortDate) + "日上传的方案文件： " + file.OldName;
        }

        //private void SetValue(IList<ProjectExtend> lst, ProjectExtendFun fun)
        //{
        //    var lstExtend = lst.Where(x => x.Fun == fun);
        //    if (lstExtend.Count() == 0)
        //        return;

        //    if (fun == ProjectExtendFun.AgentPassword)
        //    {
        //        foreach (var item in lstExtend)
        //            txtAgent.Text = item.Value;

        //        return;
        //    }

        //    CheckBoxList cb = null;

        //    switch (fun)
        //    {
        //        case ProjectExtendFun.Material:
        //            cb = cbGoods;
        //            break;
        //        case ProjectExtendFun.Type:
        //            cb = cbType;
        //            break;
        //        case ProjectExtendFun.City:
        //            cb = cbArea;
        //            break;
        //        default:
        //            return;
        //    }

        //    foreach (ListItem item in cb.Items)
        //    {
        //        if (lstExtend.FirstOrDefault(x => x.Code == item.Value) != null)
        //            item.Selected = true;
        //    }

        //}

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //string strError = string.Empty;

            //if (string.IsNullOrWhiteSpace(hfMode.Value))
            //{
            //    if (fuBook.HasFile == false || String.IsNullOrEmpty(fuBook.PostedFile.FileName))
            //    {
            //        strError = "ParseExcel:: No file found, please input a file.";
            //        return;
            //    }
            //}

            //IProjectService projectService = ServiceFactory.CreateInstance<IProjectService>();
            //Project project = null;

            //if (hfMode.Value == "edit")
            //    project = projectService.Get<Project>(hfProjectID.Value);
            //else
            //    project = new Project();

            //project.ProjectName = txtName.Text;
            //project.Money = double.Parse(txtMoney.Text);
            //project.StartTime = DateTime.Parse(txtStartTime.Text);
            //project.EndTime = DateTime.Parse(txtEndTime.Text);
            //project.Entrants = ddlCount.SelectedItem.Value.ToInt();
            //project.EntrantsName = ddlCount.SelectedItem.Text;
            //project.ProjectRequire = txtRemark.Text;
            //project.BelongNO = hfCompanyID.Value.ToInt();
            //project.BelongType = APPRole.Company;
            //project.ContactNO = hfUserID.Value.ToInt();
            //project.UrgentRemark = txtUserName.Text;

            //if (hfMode.Value == "edit")
            //{
            //    project.Status = ProjectStatus.Draft;
            //    project.BookStatus = CommonStatus.Pending;
            //    project.Type = CreateExtend(projectService, cbType.Items, ProjectExtendFun.Type, project.ProjectNO);
            //    project.Material = CreateExtend(projectService, cbGoods.Items, ProjectExtendFun.Material, project.ProjectNO);
            //    project.City = CreateExtend(projectService, cbArea.Items, ProjectExtendFun.City, project.ProjectNO);
            //    CreateExtend(projectService, null, ProjectExtendFun.AgentPassword, project.ProjectNO);
            //    projectService.Update(project, hfUserID.Value);
            //}
            //else
            //{
            //    project.Type = GetSelectedItems(cbType.Items);
            //    project.Material = GetSelectedItems(cbGoods.Items);
            //    project.City = GetSelectedItems(cbArea.Items);
            //    project.Status = ProjectStatus.Draft;
            //    project.BookStatus = CommonStatus.Pending;
            //    projectService.Create(project, hfUserID.Value);

            //    CreateExtend(projectService, null, ProjectExtendFun.AgentPassword, project.ProjectNO);
            //    CreateExtend(projectService, cbType.Items, ProjectExtendFun.Type, project.ProjectNO);
            //    CreateExtend(projectService, cbGoods.Items, ProjectExtendFun.Material, project.ProjectNO);
            //    CreateExtend(projectService, cbArea.Items, ProjectExtendFun.City, project.ProjectNO);
            //}

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && fuBook.HasFile)
            //{
            //    IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //    lst = utilService.ListFileSystem(project.ProjectNO);
            //}

            //SaveFile(fuBook, FileType.SponsorBook, lst, project.ProjectNO);

            //Response.Redirect("sponstatus.html");
        }

        private List<string> GetSelectedItems(ListItemCollection collection)
        {
            List<string> lst = new List<string>();
            foreach (ListItem item in collection)
            {
                if (item.Selected)
                    lst.Add(item.Text);
            }

            return lst;
        }

        //private List<string> CreateExtend(IProjectService service, ListItemCollection collection, ProjectExtendFun type, int projectNo)
        //{
        //    List<string> lst = new List<string>();

        //    if (hfMode.Value == "edit")
        //        service.DeleteProjectExtend(projectNo, type);

        //    if (type == ProjectExtendFun.AgentPassword)
        //    {
        //        if (txtAgent.Text.IsNullOrEmpty())
        //            return lst;

        //        ProjectExtend extend = new ProjectExtend();
        //        extend.Fun = ProjectExtendFun.AgentPassword;
        //        extend.ProjectNO = projectNo;
        //        extend.Code = "900000170";
        //        extend.Value = txtAgent.Text;
        //        service.CreateProjectExtend(extend);
        //        return lst;
        //    }

        //    foreach (ListItem item in collection)
        //    {
        //        if (item.Selected)
        //        {
        //            ProjectExtend extend = new ProjectExtend();
        //            extend.Fun = type;
        //            extend.ProjectNO = projectNo;
        //            extend.Code = item.Value;
        //            extend.Value = item.Text;

        //            service.CreateProjectExtend(extend);
        //            lst.Add(item.Text);
        //        }
        //    }

        //    return lst;
        //}

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst, int projectNo)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Company\\" + hfCompanyID.Value + "\\" + projectNo + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.TypeName = APPRole.Project;
            //fs.Belong = projectNo;
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Company/" + hfCompanyID.Value + "/" + projectNo + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = Int32.Parse(hfCompanyID.Value);

            //IUtilService service = FactoryCreator.Create<IUtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }
    }
}